import { createKeyboardMan } from 'az-keyboard-man'

const keyboardMan = createKeyboardMan()

function install(Vue) {
  Vue.prototype.$onKeydown = function () {
    const i = keyboardMan.onKeydown(...arguments)
    this.$on(
      'hook:destroyed',
      (
        (i) => () =>
          keyboardMan.onKeyRemove(i)
      )(i)
    )
    return i
  }

  Vue.prototype.$onKeyup = function () {
    const i = keyboardMan.onKeyup(...arguments)
    this.$on(
      'hook:destroyed',
      (
        (i) => () =>
          keyboardMan.onKeyRemove(i)
      )(i)
    )
    return i
  }

  Vue.prototype.$onKeyRemove = function (i) {
    return keyboardMan.onKeyRemove(i)
  }
}

class AzKeyboardMan {}

AzKeyboardMan.install = install

if (window && window.Vue) {
  window.Vue.use(AzKeyboardMan)
}

export default AzKeyboardMan
